<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./ajax.js"></script>
</head>
<body>
    姓名:<input type="text" class="username"><br>
    年龄:<input type="text" class="age"><br>
    <button>点击提交post ajax</button>
    <script>
        let btn = document.querySelector("button");
        btn.onclick = async function () {
            let username = document.querySelector(".username").value;
            let age = document.querySelector(".age").value;
            const res = await ajax({
                url:'./1.php',
                method:'post',
                dataType:'json',
                data:{
                    username,
                    age
                }
            });
           console.log(res);
            // let xhr ; 
            // if ( window.XMLHttpRequest){
            //     xhr = new XMLHttpRequest();
            // } else {
            //     xhr = new ActiveXObject();
            // }
            // xhr.open("POST","1.php",true);
            
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            // xhr.onload = function (){
            //     console.log(JSON.parse(xhr.responseText));
            // }
            // let data = `username=${username}&age=${age}`;
            // xhr.send(data);
        }
    </script>
</body>
</html>